<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>wikipedia &mdash; GOZERWAVE v0.1 BETA documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1 BETA',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="GOZERWAVE v0.1 BETA documentation" href="../index.html" />
    <link rel="prev" title="todo" href="todo.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="todo.html" title="todo"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="wikipedia">
<h1>wikipedia<a class="headerlink" href="#wikipedia" title="Permalink to this headline">¶</a></h1>
<div class="section" id="description">
<h2>description<a class="headerlink" href="#description" title="Permalink to this headline">¶</a></h2>
<p>specific wikipedia.</p>
<dl class="function">
<dt id="waveplugs.wikipedia.getwikidata">
<tt class="descclassname">waveplugs.wikipedia.</tt><tt class="descname">getwikidata</tt><big>(</big><em>url</em><big>)</big><a class="headerlink" href="#waveplugs.wikipedia.getwikidata" title="Permalink to this definition">¶</a></dt>
<dd>fetch wiki data</dd></dl>

<dl class="function">
<dt id="waveplugs.wikipedia.handle_wikipedia">
<tt class="descclassname">waveplugs.wikipedia.</tt><tt class="descname">handle_wikipedia</tt><big>(</big><em>bot</em>, <em>ievent</em><big>)</big><a class="headerlink" href="#waveplugs.wikipedia.handle_wikipedia" title="Permalink to this definition">¶</a></dt>
<dd>wikipedia &lt;what&gt; .. search wikipedia for &lt;what&gt;</dd></dl>

<dl class="function">
<dt id="waveplugs.wikipedia.searchwiki">
<tt class="descclassname">waveplugs.wikipedia.</tt><tt class="descname">searchwiki</tt><big>(</big><em>txt</em>, <em>lang='en'</em><big>)</big><a class="headerlink" href="#waveplugs.wikipedia.searchwiki" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</div>
<div class="section" id="code">
<h2>code<a class="headerlink" href="#code" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96</pre></td><td class="code"><div class="highlight"><pre><span class="c"># plugs/wikipedia.py</span>
<span class="c">#</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">     query wikipedia .. use -&lt;countrycode&gt; to select a country </span>
<span class="sd">    specific wikipedia. </span>

<span class="sd">&quot;&quot;&quot;</span>


<span class="kn">from</span> <span class="nn">gozerlib.utils.url</span> <span class="kn">import</span> <span class="n">geturl</span><span class="p">,</span> <span class="n">striphtml</span>
<span class="kn">from</span> <span class="nn">gozerlib.utils.generic</span> <span class="kn">import</span> <span class="n">splittxt</span><span class="p">,</span> <span class="n">handle_exception</span><span class="p">,</span> <span class="n">fromenc</span>
<span class="kn">from</span> <span class="nn">gozerlib.commands</span> <span class="kn">import</span> <span class="n">cmnds</span>
<span class="kn">from</span> <span class="nn">gozerlib.aliases</span> <span class="kn">import</span> <span class="n">aliasset</span>
<span class="kn">from</span> <span class="nn">gozerlib.examples</span> <span class="kn">import</span> <span class="n">examples</span>
<span class="kn">from</span> <span class="nn">gozerlib.utils.rsslist</span> <span class="kn">import</span> <span class="n">rsslist</span>

<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">quote</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="n">wikire</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&#39;start content(.*?)end content&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">M</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">searchwiki</span><span class="p">(</span><span class="n">txt</span><span class="p">,</span> <span class="n">lang</span><span class="o">=</span><span class="s">&#39;en&#39;</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">txt</span><span class="o">.</span><span class="n">split</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;-&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">!=</span> <span class="mf">3</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lang</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mf">1</span><span class="p">:]</span>
            <span class="k">continue</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">txt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot;-</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">lang</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">txt</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
    <span class="n">what</span> <span class="o">=</span> <span class="n">txt</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span> <span class="s">&#39;_&#39;</span><span class="p">)</span>
    <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/Special:Export/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">what</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
    <span class="n">url2</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">what</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">getwikidata</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">txt</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">None</span>
    <span class="k">if</span> <span class="s">&#39;from other capitalisation&#39;</span> <span class="ow">in</span> <span class="n">txt</span><span class="p">:</span>
        <span class="n">what</span> <span class="o">=</span> <span class="n">what</span><span class="o">.</span><span class="n">title</span><span class="p">()</span>
        <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/Special:Export/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">what</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
        <span class="n">url2</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">what</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
        <span class="n">txt</span> <span class="o">=</span> <span class="n">getwikidata</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="k">if</span> <span class="s">&#39;#REDIRECT&#39;</span> <span class="ow">in</span> <span class="n">txt</span> <span class="ow">or</span> <span class="s">&#39;#redirect&#39;</span> <span class="ow">in</span> <span class="n">txt</span><span class="p">:</span>
        <span class="n">redir</span> <span class="o">=</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">txt</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mf">1</span><span class="p">:])</span>
        <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/Special:Export/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">redir</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
        <span class="n">url2</span> <span class="o">=</span> <span class="s">&#39;http://</span><span class="si">%s</span><span class="s">.wikipedia.org/wiki/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lang</span><span class="p">,</span> \
<span class="n">quote</span><span class="p">(</span><span class="n">redir</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)))</span>
        <span class="n">txt</span> <span class="o">=</span> <span class="n">getwikidata</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">txt</span><span class="p">,</span> <span class="n">url2</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">getwikidata</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; fetch wiki data &quot;&quot;&quot;</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">fromenc</span><span class="p">(</span><span class="n">geturl</span><span class="p">(</span><span class="n">url</span><span class="p">))</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">result</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">rsslist</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">res</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">txt</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="s">&#39;text&#39;</span><span class="p">]</span>
            <span class="k">break</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">pass</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">txt</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="c">#txt = re.sub(&#39;\[\[Image:([^\[\]]+|\[\[[^\]]+\]\])*\]\]&#39;, &#39;&#39;, txt)</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">txt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;[[&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">txt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;]]&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;\s+&#39;</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="p">,</span> <span class="n">txt</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">txt</span>

<span class="k">def</span> <span class="nf">handle_wikipedia</span><span class="p">(</span><span class="n">bot</span><span class="p">,</span> <span class="n">ievent</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; wikipedia &lt;what&gt; .. search wikipedia for &lt;what&gt; &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">ievent</span><span class="o">.</span><span class="n">rest</span><span class="p">:</span>
        <span class="n">ievent</span><span class="o">.</span><span class="n">missing</span><span class="p">(</span><span class="s">&#39;&lt;what&gt;&#39;</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">searchwiki</span><span class="p">(</span><span class="n">ievent</span><span class="o">.</span><span class="n">rest</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">res</span><span class="p">:</span>
        <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="s">&#39;no result found&#39;</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="n">txt</span><span class="p">,</span> <span class="n">url</span> <span class="o">=</span> <span class="n">res</span>
    <span class="n">prefix</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> ===&gt; &#39;</span> <span class="o">%</span> <span class="n">url</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">splittxt</span><span class="p">(</span><span class="n">striphtml</span><span class="p">(</span><span class="n">txt</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
    <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>

<span class="n">cmnds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;wikipedia&#39;</span><span class="p">,</span> <span class="n">handle_wikipedia</span><span class="p">,</span> <span class="s">&#39;USER&#39;</span><span class="p">)</span>
<span class="n">examples</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;wikipedia&#39;</span><span class="p">,</span> <span class="s">&#39;wikipedia [&quot;-&quot; &lt;countrycode&gt;] &lt;what&gt; .. search </span><span class="se">\</span>
<span class="s">wikipedia for &lt;what&gt;&#39;</span><span class="p">,</span><span class="s">&#39;1) wikipedia gozerbot 2) wikipedia -nl bot&#39;</span><span class="p">)</span>
<span class="n">aliasset</span><span class="p">(</span><span class="s">&#39;wiki&#39;</span><span class="p">,</span> <span class="s">&#39;wikipedia&#39;</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">wikipedia</a><ul>
<li><a class="reference external" href="#description">description</a></li>
<li><a class="reference external" href="#code">code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="todo.html"
                                  title="previous chapter">todo</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/waveplugs/wikipedia.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="todo.html" title="todo"
             >previous</a> |</li>
        <li><a href="../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Bart Thate.
      Last updated on Nov 14, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>